App.module.Language.Code.Add = Ext.extend(Ext.util.Observable, {
	init: function(grid){
		this.grid = grid;
		
		grid.on('render', function(grid){
			
			var bt = new Ext.Button({
				tooltip: 'Add'
				,iconCls: 'icon-add-16x16'
				,handler: this.showWin
				,scope: this	
			}, this);
			
			grid.getTopToolbar().add(bt);
			
		}, this);	
	}
	
	,getForm: function(){
		if(this.form) return this.form;
		
		var form = new Ext.FormPanel({
			frame: false
			,baseCls: 'x-plain'
			,monitorValid: true
			,defaults: {xtype: 'textfield', anchor: '0'}
			,items: [
				{
					name: 'name'
					,fieldLabel: 'Name'
					,allowBlank: false
					,regex: /\w/	
				},{
					name: 'description'
					,fieldLabel: 'Description'	
				}
			]
			,buttons: [
				{
					text: 'Save'
					,formBind: true
					,scope: this
					,handler: function(){
						var values = form.getForm().getValues();
						
						form.el.mask('Processing...');
						API.language.addCode(values, function(r){
							form.el.unmask();
							
							values.id = r.id;
							this.grid.getStore().insert(0, new Ext.data.Record(values, r.id));
						}, this);
					}	
				},{
					text: 'Reset'
					,handler: function(){form.getForm().reset();}	
				},{
					text: 'Close'
					,scope: this
					,handler: function(){this.getWin().hide();}	
				}
			]
		});
		
		this.form = form;
		return this.form;
	}
	
	,getWin: function(){
		if(this.win) return this.win;
		
		this.win = new App.SubWindow({
			title: 'Add Code'
			,renderTo: this.grid.ownerCt.body.id
			,bodyStyle: 'padding: 5px'
			,border: false
			,plain: true
			,items: this.getForm()	
		});
		
		return this.win;	
	}
	
	,showWin: function(bt){
		this.getWin().show(bt.id);	
	}	
});